package com.google.code.chaotic.client.ui;

import com.allen_sauer.gwt.log.client.Log;
import com.google.code.chaotic.client.ClientSessionManager;
import com.google.code.chaotic.client.LoginDto;
import com.google.code.chaotic.client.Util;
import com.google.code.chaotic.rpc.AuthSvc;
import com.google.code.chaotic.rpc.AuthSvcAsync;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class ChaosEntryPoint implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	private final AuthSvcAsync authSvc = GWT.create(AuthSvc.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		Log.setUncaughtExceptionHandler();

		String sessionId = ClientSessionManager.getId();
		if (sessionId == null)
			sessionId = Cookies.getCookie("sid");
		if (sessionId == null)
			Window.open(GWT.getHostPageBaseURL() + "Login.html", "_self", "");
		else
			checkValidityOfSessionId(sessionId);

		// ContentPanel cp = new ContentPanel();
		// cp.setHeading("Folder Contents");
		// cp.setSize(250, 140);
		// cp.setPosition(10, 10);
		// cp.setCollapsible(true);
		// cp.setFrame(true);
		// cp.setBodyStyle("backgroundColor: white;");
		// cp.getHeader().addTool(new ToolButton("x-tool-gear"));
		// cp.getHeader().addTool(new ToolButton("x-tool-close"));
		// cp.addText("A Bogus Text!");// getBogusText());
		// cp.addButton(new Button("Ok"));
		// cp.setIconStyle("tree-folder-open");
		// RootPanel.get().add(cp);
		// cp.layout();
	}

	private void checkValidityOfSessionId(String sessionId) {
		authSvc.getLoginDto(sessionId, new AsyncCallback<LoginDto>() {
			@Override
			public void onSuccess(final LoginDto result) {
				DeferredCommand.addCommand(new Command() {
					@Override
					public void execute() {
						ClientSessionManager csm = ClientSessionManager
								.getInstance();
						csm.setSessionId(result.getSessionId());
						csm.setUserNoPass(result.getUserNoPass());
						deferedModuleLoad();
					}
				});
			}

			@Override
			public void onFailure(Throwable th) {
				Log.error(th.toString(), th);
				Cookies.removeCookie("sid");
				Window.open(GWT.getHostPageBaseURL() + "Login.html", //
						"_self", "");
			}
		});
	}

	private void deferedModuleLoad() {
		// final ServiceMgmtPanel dockPanel = new ServiceMgmtPanel();
		final ChaosPanel dockPanel = new ChaosPanel();
		final RootPanel rootPanel = RootPanel.get("root");
		Util.clearElement(rootPanel.getElement());
		rootPanel.add(dockPanel, -1, -1);
		// dockPanel.requestFocus();

		Window.addResizeHandler(new ResizeHandler() {
			@Override
			public void onResize(ResizeEvent event) {
				maximizeRootPanel(rootPanel);
			}
		});
		maximizeRootPanel(rootPanel);
	}

	private void maximizeRootPanel(RootPanel panel) {
		panel.setPixelSize( //
				Window.getClientWidth(), //
				Window.getClientHeight());
	}
}
